<?php

namespace app\index\controller;

use app\index\common\Base;
use think\Db;
use think\Session;

class Index extends Base
{
    // public function index()
    // {
        
    //     return $this->fetch();
    // }
        public function index()
    {
    	//return "11";
    	$server_url = $_SERVER['SERVER_NAME']?"http://".$_SERVER['SERVER_NAME']:"http://".$_SERVER['HTTP_HOST'];
    	$this->redirect($server_url.'/mobile/login');
    }

    // 获取验证码
    public function get_message()
    {
        $data = $this->request->param();
        if (!isset($data['mobile'])) {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }

        if (empty($data['mobile'])) {
            return json(['status' => 0, 'msg' => '手机号不能为空']);
        }
        $info = send_mobile_message($data['mobile']);

        if ($info['status'] == 1) {
            return json(['status' => 1, 'msg' => '短信发送成功']);
        }

        return json(['status' => 0, 'msg' => '短信发送失败']);
    }

    // 验证产品序列号
    public function verify()
    {
        $data = $this->request->param();
        if (!isset($data['mobile'])) {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }

        if (empty($data['mobile'])) {
            return json(['status' => 0, 'msg' => '手机号不能为空']);
        }

        if (!isset($data['code']) || empty($data['code'])) {
            return json(['status' => 0, 'msg' => '请输入验证码']);
        }

        if ($data['code'] != Session::get('sh_message')) {
            return json(['status' => 0, 'msg' => '验证码有误']);
        }

        $info = Db::name('tagslogs')->where('sn', $data['sn'])->find();

        if (empty($info)) {
            return json(['status' => 0, 'msg' => '对不起！找不到您所验证的产品，谨防假冒']);
        }

        // 通过IP获取地址信息
        $address = getlocation($this->request->ip());

        // 存验证记录
        $checklogs['tagslogs_id'] = $info['id'];
        $checklogs['addtime'] = time();
        $checklogs['province'] = $address['province'];
        $checklogs['city'] = $address['city'];
        $checklogs['type'] = 1;
        $checklogs['telephone'] = $data['mobile'];

        // 获取标签主表
        $goodstags = Db::name('goodstags')->where('id', $info['goodstags_id'])->find();
        // 获取商品信息
        $good = Db::name('good')->where('id', $goodstags['good_id'])->find();

        if (empty($good)) {
            return json(['status' => 0, 'msg' => '商品不存在']);
        }
        $checklogs['good_id'] = $good['id'];

        $rest = Db::name('checklogs')->insert($checklogs);

        if ($rest) {
            return json(['status' => 1, 'msg' => '恭喜您！您所验证的产品是正品！']);
        }

        return json(['status' => 0, 'msg' => '数据库异常请稍后再试']);
    }
}
